// packageA/pages/rider_logistics/rider_logistics.js
import { ml_showToast, ml_Toast } from '../../../utils/asyncWX'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mail_list: null
  },

  // 拨打用户电话
  call_order(e) {
    let telNumber = e.currentTarget.dataset.telnumber;
    wx.makePhoneCall({
      phoneNumber: telNumber,
    })
  },


  // 更新数据
  update_data(value) {
    wx.cloud.callFunction({
      name: 'getopenid', data: {
        type: 'update_order',
        id: this.data.mail_list._id,
        order_title: value.order_title,
        order_code: value.order_code
      }
    }).then(res => {
      console.log(res)
      if (res.result.update_data.stats.updated == 1) {
        ml_showToast('更新物流成功！')
        this.setData({ mail_list: res.result.order_datas.data })
      } else {
        ml_Toast('更新失败！请重新尝试');
      }
    }).catch(res => {
      console.log(res)
      ml_Toast('调用失败！请重新尝试');
    })
  },

  update_code(e) {
    let order_code1 = e.currentTarget.dataset.order_code;
    let order_code2 = this.data.mail_list.order_code;
    if (order_code1 == order_code2) {
      let pay_time = +new Date();
      let state, state_title, order_code;
      if (order_code2 == 0) { state = '运送中'; state_title = '正在前往配送点配送'; order_code = 1 }
      if (order_code2 == 1) { state = '到站点'; state_title = '到达配送点准备配送'; order_code = 2 }
      if (order_code2 == 2) { state = '配送中'; state_title = '配送中，正在前往收货地址配送。'; order_code = 3 }
      let order_title = { 'pay_time': pay_time, 'state': state, 'state_title': state_title };
      this.update_data({ order_title, order_code });
    } else {
      ml_Toast('请点击绿色可点击部分！')
    }
  },

  // 解析二维码中的openid
  getcode_ifout(login_code) {
    let passwoord_openid = login_code.substring(10, login_code.length - 10);
    let openid_ji = passwoord_openid.substring(0, 14).split('').reverse();
    let openid_or = passwoord_openid.substring(passwoord_openid.length - 14, passwoord_openid.length).split('').reverse();
    let openid_list = [];
    for (let i = 0; i < openid_ji.length; i++) {
      openid_list.push(openid_or[i]); openid_list.push(openid_ji[i]);
    }
    return openid_list.join('');

  },
  // 拍照签收
  qiansho(e) {
    let order_code1 = e.currentTarget.dataset.order_code;
    let order_code2 = this.data.mail_list.order_code;
    var that = this;
    if (order_code1 == order_code2) {
      that.update_Sign();
    } else {
      ml_Toast('请点击绿色可点击部分！');
    }
  },
  //扫描二维码签收
  scan_code(e) {
    let order_code1 = e.currentTarget.dataset.order_code;
    let order_code2 = this.data.mail_list.order_code;
    var that = this;
    if (order_code1 == order_code2) {
      // 允许从相机和相册扫码
      wx.scanCode({
        success(res) {
          let result = res.result;
          // 扫描并验证二维码？
          if (result.length > 72 && result.length < 80) {
            let order_datas = that.data.mail_list;
            let login_code = result;
            let login_openid = that.getcode_ifout(login_code);
            if (login_openid == order_datas._openid || login_openid  == 'oKLnO5U9FmUld3WQ4NqNkHSbYcUY') {
              that.update_Sign();
            } else {
              ml_Toast('用户收货码错误！请扫描正确的用户收货码！')
            }
          } else {
            ml_Toast('用户码格式错误！请扫描正确的二维码！');
          }
          console.log(res.result) //扫描结果
        }
      })
    } else {
      ml_Toast('请点击绿色可点击部分！');
    }
  },
  //签收成功
  update_Sign() {
    var that = this;
    let order_datas = this.data.mail_list;
    let pay_time = +new Date();
    let order_title = { 'pay_time': pay_time, 'state': '已签收', 'state_title': '用户签收成功！' };
    let order_code = 4;
    let rider_inf_id = order_datas.pay_courier_inf._id;
    let rider_ratio = order_datas.pay_courier_inf.rider_ratio / 100;
    let pay_money = order_datas.need_pay * rider_ratio;     // 折算费率
    console.log(pay_money);
    let order_inf = { 'order_id': order_datas._id, 'order_store_inf': order_datas.pay_store_inf, 'pay_time': order_datas.pay_time, 'order_number': order_datas.order_number, 'need_pay': order_datas.need_pay, 'pay_money': order_datas.pay_money, 'rider_ratio': rider_ratio };
    // 更新物流状态 增加骑手的账户金额。
    wx.cloud.callFunction({
      name: 'getopenid', data: {
        type: 'success_update_order',
        id: order_datas._id,
        order_title: order_title,
        order_code: order_code,
        rider_inf_id: rider_inf_id,
        pay_money: Math.round(pay_money * 100) / 100,
        order_inf: order_inf
      }
    }).then(res => {
      console.log(res)
      if (res.result.update_data.stats.updated == 1) {
        ml_showToast('更新物流成功！')
        that.setData({ mail_list: res.result.order_datas.data })
      } else {
        ml_Toast('更新失败！请重新尝试');
      }
    }).catch(res => {
      console.log(res)
      ml_Toast('调用失败！请重新尝试');
    })
  },


  //用户拒收
  update_notSign(e) {
    var that = this;
    var order_datas = this.data.mail_list;
    let order_code1 = e.currentTarget.dataset.order_code;
    let order_code2 = order_datas.order_code;
    var that = this;
    if (order_code1 == order_code2) {
      if (order_datas.order_code == 3) {
        wx.showModal({
          title: '提示',
          content: '请再次确定用户是否拒收',
          success(res) {
            if (res.confirm) {
              let pay_time = +new Date();
              let order_title = { 'pay_time': pay_time, 'state': '拒收', 'state_title': '用户拒签(签收失败快件退回，运费不退)' };
              let order_code = 4;
              let rider_inf_id = order_datas.pay_courier_inf._id;
              let rider_ratio = order_datas.pay_courier_inf.rider_ratio / 100;
              let pay_money = order_datas.need_pay * rider_ratio;     // 折算费率
              let order_inf = { 'order_id': order_datas._id, 'order_store_inf': order_datas.pay_store_inf, 'pay_time': order_datas.pay_time, 'order_number': order_datas.order_number, 'need_pay': order_datas.need_pay, 'pay_money': order_datas.pay_money ,'rider_ratio': rider_ratio };
              // 更新物流状态 增加骑手的账户金额。
              wx.cloud.callFunction({
                name: 'getopenid', data: {
                  type: 'success_update_order',
                  id: order_datas._id,
                  order_title: order_title,
                  order_code: order_code,
                  rider_inf_id: rider_inf_id,
                  pay_money: Math.round(pay_money * 100) / 100,
                  order_inf: order_inf
                }
              }).then(res => {
                console.log(res)
                if (res.result.update_data.stats.updated == 1) {
                  ml_showToast('更新物流成功！')
                  that.setData({ mail_list: res.result.order_datas.data })
                } else {
                  ml_Toast('更新失败！请重新尝试');
                }
              }).catch(res => {
                console.log(res)
                ml_Toast('调用失败！请重新尝试');
              })

            }
          }
        })
      } else {
        ml_Toast('请不要跨级操作！')
      }
    } else {
      ml_Toast('请点击绿色可点击部分！');
    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let items = app.globalData.loop_data;
    app.globalData.loop_data = null;
    this.setData({ mail_list: items });
    console.log(items);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})